﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.OleDb;
using Moon.DBUtility;

namespace DAL
{
    public partial class NodeDAL
    {
        #region  Method
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int Id)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from t_node");
            strSql.Append(" where Id=@Id");
            OleDbParameter[] parameters = {
					new OleDbParameter("@Id", OleDbType.Integer,4)
			};
            parameters[0].Value = Id;

            return DbHelperOleDb.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(Model.NodeInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into t_node(");
            strSql.Append("NodeName,ShowTitle,NodeImage,IsShow,Template,ParentID,MateKey,MateMark,Url,Content,IsHTML,HeardSequence,IsHeard,ClassID,OrderBy,UpdateDate,ModelType)");
            strSql.Append(" values (");
            strSql.Append("@NodeName,@ShowTitle,@NodeImage,@IsShow,@Template,@ParentID,@MateKey,@MateMark,@Url,@Content,@IsHTML,@HeardSequence,@IsHeard,@ClassID,@OrderBy,@UpdateDate,@ModelType)");
            OleDbParameter[] parameters = {
					new OleDbParameter("@NodeName", OleDbType.VarChar,100),
					new OleDbParameter("@ShowTitle", OleDbType.VarChar,100),
					new OleDbParameter("@NodeImage", OleDbType.VarChar,255),
					new OleDbParameter("@IsShow", OleDbType.Boolean,1),
					new OleDbParameter("@Template", OleDbType.Integer,4),
					new OleDbParameter("@ParentID", OleDbType.Integer,4),
					new OleDbParameter("@MateKey", OleDbType.VarChar,255),
					new OleDbParameter("@MateMark", OleDbType.VarChar,255),
					new OleDbParameter("@Url", OleDbType.VarChar,255),
					new OleDbParameter("@Content", OleDbType.VarChar,0),
					new OleDbParameter("@IsHTML", OleDbType.Boolean,1),
					new OleDbParameter("@HeardSequence", OleDbType.VarChar,50),
					new OleDbParameter("@IsHeard", OleDbType.Boolean,1),
					new OleDbParameter("@ClassID", OleDbType.Integer,4),
					new OleDbParameter("@OrderBy", OleDbType.Integer,4),
					new OleDbParameter("@UpdateDate", OleDbType.Date),
					new OleDbParameter("@ModelType", OleDbType.VarChar,50)};
            parameters[0].Value = model.NodeName;
            parameters[1].Value = model.ShowTitle;
            parameters[2].Value = model.NodeImage;
            parameters[3].Value = model.IsShow;
            parameters[4].Value = model.Template;
            parameters[5].Value = model.ParentID;
            parameters[6].Value = model.MateKey;
            parameters[7].Value = model.MateMark;
            parameters[8].Value = model.Url;
            parameters[9].Value = model.Content;
            parameters[10].Value = model.IsHTML;
            parameters[11].Value = model.HeardSequence;
            parameters[12].Value = model.IsHeard;
            parameters[13].Value = model.ClassID;
            parameters[14].Value = model.OrderBy;
            parameters[15].Value = model.UpdateDate;
            parameters[16].Value = model.ModelType;

            int rows = DbHelperOleDb.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Model.NodeInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update t_node set ");
            strSql.Append("NodeName=@NodeName,");
            strSql.Append("ShowTitle=@ShowTitle,");
            strSql.Append("NodeImage=@NodeImage,");
            strSql.Append("IsShow=@IsShow,");
            strSql.Append("Template=@Template,");
            strSql.Append("ParentID=@ParentID,");
            strSql.Append("MateKey=@MateKey,");
            strSql.Append("MateMark=@MateMark,");
            strSql.Append("Url=@Url,");
            strSql.Append("Content=@Content,");
            strSql.Append("IsHTML=@IsHTML,");
            strSql.Append("HeardSequence=@HeardSequence,");
            strSql.Append("IsHeard=@IsHeard,");
            strSql.Append("ClassID=@ClassID,");
            strSql.Append("OrderBy=@OrderBy,");
            strSql.Append("UpdateDate=@UpdateDate,");
            strSql.Append("ModelType=@ModelType");
            strSql.Append(" where Id=@Id");
            OleDbParameter[] parameters = {
					new OleDbParameter("@NodeName", OleDbType.VarChar,100),
					new OleDbParameter("@ShowTitle", OleDbType.VarChar,100),
					new OleDbParameter("@NodeImage", OleDbType.VarChar,255),
					new OleDbParameter("@IsShow", OleDbType.Boolean,1),
					new OleDbParameter("@Template", OleDbType.Integer,4),
					new OleDbParameter("@ParentID", OleDbType.Integer,4),
					new OleDbParameter("@MateKey", OleDbType.VarChar,255),
					new OleDbParameter("@MateMark", OleDbType.VarChar,255),
					new OleDbParameter("@Url", OleDbType.VarChar,255),
					new OleDbParameter("@Content", OleDbType.VarChar,0),
					new OleDbParameter("@IsHTML", OleDbType.Boolean,1),
					new OleDbParameter("@HeardSequence", OleDbType.VarChar,50),
					new OleDbParameter("@IsHeard", OleDbType.Boolean,1),
					new OleDbParameter("@ClassID", OleDbType.Integer,4),
					new OleDbParameter("@OrderBy", OleDbType.Integer,4),
					new OleDbParameter("@UpdateDate", OleDbType.Date),
					new OleDbParameter("@ModelType", OleDbType.VarChar,50),
					new OleDbParameter("@Id", OleDbType.Integer,4)};
            parameters[0].Value = model.NodeName;
            parameters[1].Value = model.ShowTitle;
            parameters[2].Value = model.NodeImage;
            parameters[3].Value = model.IsShow;
            parameters[4].Value = model.Template;
            parameters[5].Value = model.ParentID;
            parameters[6].Value = model.MateKey;
            parameters[7].Value = model.MateMark;
            parameters[8].Value = model.Url;
            parameters[9].Value = model.Content;
            parameters[10].Value = model.IsHTML;
            parameters[11].Value = model.HeardSequence;
            parameters[12].Value = model.IsHeard;
            parameters[13].Value = model.ClassID;
            parameters[14].Value = model.OrderBy;
            parameters[15].Value = model.UpdateDate;
            parameters[16].Value = model.ModelType;
            parameters[17].Value = model.Id;

            int rows = DbHelperOleDb.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int Id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from t_node ");
            strSql.Append(" where Id=@Id");
            OleDbParameter[] parameters = {
					new OleDbParameter("@Id", OleDbType.Integer,4)
			};
            parameters[0].Value = Id;

            int rows = DbHelperOleDb.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string Idlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from t_node ");
            strSql.Append(" where Id in (" + Idlist + ")  ");
            int rows = DbHelperOleDb.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.NodeInfo GetModel(int Id)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Id,NodeName,ShowTitle,NodeImage,IsShow,Template,ParentID,MateKey,MateMark,Url,Content,IsHTML,HeardSequence,IsHeard,ClassID,OrderBy,UpdateDate,ModelType from t_node ");
            strSql.Append(" where Id=@Id");
            OleDbParameter[] parameters = {
					new OleDbParameter("@Id", OleDbType.Integer,4)
			};
            parameters[0].Value = Id;

            Model.NodeInfo model = new Model.NodeInfo();
            DataSet ds = DbHelperOleDb.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["Id"] != null && ds.Tables[0].Rows[0]["Id"].ToString() != "")
                {
                    model.Id = int.Parse(ds.Tables[0].Rows[0]["Id"].ToString());
                }
                if (ds.Tables[0].Rows[0]["NodeName"] != null && ds.Tables[0].Rows[0]["NodeName"].ToString() != "")
                {
                    model.NodeName = ds.Tables[0].Rows[0]["NodeName"].ToString();
                }
                if (ds.Tables[0].Rows[0]["ShowTitle"] != null && ds.Tables[0].Rows[0]["ShowTitle"].ToString() != "")
                {
                    model.ShowTitle = ds.Tables[0].Rows[0]["ShowTitle"].ToString();
                }
                if (ds.Tables[0].Rows[0]["NodeImage"] != null && ds.Tables[0].Rows[0]["NodeImage"].ToString() != "")
                {
                    model.NodeImage = ds.Tables[0].Rows[0]["NodeImage"].ToString();
                }
                if (ds.Tables[0].Rows[0]["IsShow"] != null && ds.Tables[0].Rows[0]["IsShow"].ToString() != "")
                {
                    if ((ds.Tables[0].Rows[0]["IsShow"].ToString() == "1") || (ds.Tables[0].Rows[0]["IsShow"].ToString().ToLower() == "true"))
                    {
                        model.IsShow = true;
                    }
                    else
                    {
                        model.IsShow = false;
                    }
                }
                if (ds.Tables[0].Rows[0]["Template"] != null && ds.Tables[0].Rows[0]["Template"].ToString() != "")
                {
                    model.Template = int.Parse(ds.Tables[0].Rows[0]["Template"].ToString());
                }
                if (ds.Tables[0].Rows[0]["ParentID"] != null && ds.Tables[0].Rows[0]["ParentID"].ToString() != "")
                {
                    model.ParentID = int.Parse(ds.Tables[0].Rows[0]["ParentID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["MateKey"] != null && ds.Tables[0].Rows[0]["MateKey"].ToString() != "")
                {
                    model.MateKey = ds.Tables[0].Rows[0]["MateKey"].ToString();
                }
                if (ds.Tables[0].Rows[0]["MateMark"] != null && ds.Tables[0].Rows[0]["MateMark"].ToString() != "")
                {
                    model.MateMark = ds.Tables[0].Rows[0]["MateMark"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Url"] != null && ds.Tables[0].Rows[0]["Url"].ToString() != "")
                {
                    model.Url = ds.Tables[0].Rows[0]["Url"].ToString();
                }
                if (ds.Tables[0].Rows[0]["Content"] != null && ds.Tables[0].Rows[0]["Content"].ToString() != "")
                {
                    model.Content = ds.Tables[0].Rows[0]["Content"].ToString();
                }
                if (ds.Tables[0].Rows[0]["IsHTML"] != null && ds.Tables[0].Rows[0]["IsHTML"].ToString() != "")
                {
                    if ((ds.Tables[0].Rows[0]["IsHTML"].ToString() == "1") || (ds.Tables[0].Rows[0]["IsHTML"].ToString().ToLower() == "true"))
                    {
                        model.IsHTML = true;
                    }
                    else
                    {
                        model.IsHTML = false;
                    }
                }
                if (ds.Tables[0].Rows[0]["HeardSequence"] != null && ds.Tables[0].Rows[0]["HeardSequence"].ToString() != "")
                {
                    model.HeardSequence = ds.Tables[0].Rows[0]["HeardSequence"].ToString();
                }
                if (ds.Tables[0].Rows[0]["IsHeard"] != null && ds.Tables[0].Rows[0]["IsHeard"].ToString() != "")
                {
                    if ((ds.Tables[0].Rows[0]["IsHeard"].ToString() == "1") || (ds.Tables[0].Rows[0]["IsHeard"].ToString().ToLower() == "true"))
                    {
                        model.IsHeard = true;
                    }
                    else
                    {
                        model.IsHeard = false;
                    }
                }
                if (ds.Tables[0].Rows[0]["ClassID"] != null && ds.Tables[0].Rows[0]["ClassID"].ToString() != "")
                {
                    model.ClassID = int.Parse(ds.Tables[0].Rows[0]["ClassID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["OrderBy"] != null && ds.Tables[0].Rows[0]["OrderBy"].ToString() != "")
                {
                    model.OrderBy = int.Parse(ds.Tables[0].Rows[0]["OrderBy"].ToString());
                }
                if (ds.Tables[0].Rows[0]["UpdateDate"] != null && ds.Tables[0].Rows[0]["UpdateDate"].ToString() != "")
                {
                    model.UpdateDate = DateTime.Parse(ds.Tables[0].Rows[0]["UpdateDate"].ToString());
                }
                if (ds.Tables[0].Rows[0]["ModelType"] != null && ds.Tables[0].Rows[0]["ModelType"].ToString() != "")
                {
                    model.ModelType = ds.Tables[0].Rows[0]["ModelType"].ToString();
                }
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Id,NodeName,ShowTitle,NodeImage,IsShow,Template,ParentID,MateKey,MateMark,Url,Content,IsHTML,HeardSequence,IsHeard,ClassID,OrderBy,UpdateDate,ModelType ");
            strSql.Append(" FROM t_node ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperOleDb.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM t_node ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperOleDb.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string[] orderby, int startIndex, int endIndex)
        {
            //獲得查詢條數
            int count = endIndex - startIndex;
            //查詢條件
            string where = "";
            //內排序條件
            string Inorderby = "order by ID desc";
            //外排序條件
            string Outorderby = "order by ID ASC";
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                where += " where 1=1 " + strWhere;
                if (orderby.Length > 1)
                {
                    //0號位置存取排序字段
                    Inorderby = "order by " + orderby[0].Trim() + " " + orderby[1].Trim();
                    //1號位置為排序字段
                    if (orderby[1].Trim().ToLower().Equals("desc"))
                    {
                        Outorderby = "order by " + orderby[0].Trim() + " asc";
                    }
                    else
                    {
                        Outorderby = "order by " + orderby[0].Trim() + " desc";
                    }
                }
            }
            else
            {
                if (orderby.Length > 1)
                {
                    //0號位置存取排序字段
                    Inorderby = "order by " + orderby[0].Trim() + " " + orderby[1].Trim();
                    //1號位置為排序字段
                    if (orderby[1].Trim().ToLower().Equals("desc"))
                    {
                        Outorderby = "order by " + orderby[0].Trim() + " asc";
                    }
                    else
                    {
                        Outorderby = "order by " + orderby[0].Trim() + " desc";
                    }
                }
            }

            StringBuilder strSql = new StringBuilder();
            strSql.AppendFormat("select * from (select top {0} * from (select top {1} * from t_node {2} {3} ) {4}) {3}", count, endIndex, where, Inorderby, Outorderby);

            return DbHelperOleDb.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            OleDbParameter[] parameters = {
                    new OleDbParameter("@tblName", OleDbType.VarChar, 255),
                    new OleDbParameter("@fldName", OleDbType.VarChar, 255),
                    new OleDbParameter("@PageSize", OleDbType.Integer),
                    new OleDbParameter("@PageIndex", OleDbType.Integer),
                    new OleDbParameter("@IsReCount", OleDbType.Boolean),
                    new OleDbParameter("@OrderType", OleDbType.Boolean),
                    new OleDbParameter("@strWhere", OleDbType.VarChar,1000),
                    };
            parameters[0].Value = "t_node";
            parameters[1].Value = "Id";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperOleDb.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  Method
    }
}
